﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.Exceptions;
using Autodesk.Revit.UI;
using Autodesk.Revit.UI.Selection;
using YArchitech.Controls;
using HYFloorPipeModule;

namespace YArchitech.Plumbing
{
	[Transaction(TransactionMode.Manual)]
	[Regeneration(RegenerationOption.Manual)]
	public class DrawSystem : IExternalCommand
	{
		public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
		{
			UIDocument activeUIDocument = commandData.Application.ActiveUIDocument;
			Document document = activeUIDocument.Document;
			Selection selection = activeUIDocument.Selection;
			Transaction transaction = null;
			Result result;
			try
			{
				transaction = new Transaction(document, "绘制系统图");
				transaction.Start();
				MainCalculationForm mainCalculationForm = new MainCalculationForm(true);
				for (;;)
				{
					mainCalculationForm.ShowDialog();
					if (mainCalculationForm.DialogResult != DialogResult.Abort)
					{
						break;
					}
					ViewPlan viewPlan = document.ActiveView as Autodesk.Revit.DB.ViewPlan;
                    if (viewPlan == null || (int)viewPlan.ViewType != 1)
					{
						YJKMessageBox.Show("请在平面视图下运行该命令！");
					}
					else
					{
						InBuildSystem buildSystem = mainCalculationForm.buildSystem;
						OptionData option = mainCalculationForm.option;
						this.GetSystem(document, selection, buildSystem, option);
					}
				}
				result = Autodesk.Revit.UI.Result.Succeeded;
			}
			catch (Autodesk.Revit.Exceptions.OperationCanceledException)
			{
				result = Autodesk.Revit.UI.Result.Succeeded;
			}
			catch (Exception ex)
			{
				message = ex.Message;
				result = Autodesk.Revit.UI.Result.Failed;
			}
			finally
			{
				if (transaction != null)
				{
					TransactionStatus status = transaction.GetStatus();
                    if ((int)status == 1)
					{
						transaction.Commit();
					}
                    else if ((int)status == 5)
					{
						transaction.RollBack();
					}
				}
			}
			return result;
		}

		private void GetSystem(Document doc, Selection sel, InBuildSystem buildSystem, OptionData option)
		{
			try
			{
				string[] lineNames = this.GetLineNames(doc);
				if (lineNames == null)
				{
					YJKMessageBox.Show("没有线样式，请新建线样式。");
				}
				else
				{
					Dictionary<int, string> dictionary = new Dictionary<int, string>();
					Dictionary<int, LevelMarkInfo> dictionary2 = new Dictionary<int, LevelMarkInfo>();
					int count = buildSystem.SupplyTmPipe.Count;
					for (int i = 0; i < count; i++)
					{
						dictionary.Add(i + 1, "DN" + buildSystem.SupplyTmPipe[i].Diameter);
					}
					for (int j = count; j < 2 * count; j++)
					{
						dictionary.Add(j + 1, "DN" + buildSystem.ReturnTmPipe[j - count].Diameter);
					}
					for (int k = 0; k < buildSystem.SrpSystems.Count; k++)
					{
						int count2 = buildSystem.SrpSystems[k].FloorSystems.Count;
						Dictionary<int, string> dictionary3 = new Dictionary<int, string>();
						Dictionary<int, int> dictionary4 = new Dictionary<int, int>();
						for (int l = 0; l < count2; l++)
						{
							dictionary3.Add(l + 1, "DN" + buildSystem.SrpSystems[k].SupplyPipes[l].Diameter);
							dictionary4.Add(l + 1, 2);
						}
						for (int m = count2; m < 2 * count2; m++)
						{
							dictionary3.Add(m + 1, "DN" + buildSystem.SrpSystems[k].ReturnPipes[m - count2].Diameter);
						}
						dictionary2.Add(k + 1, new LevelMarkInfo(count2, dictionary3, dictionary4));
					}
					HeatingPrincipleDiagramPrameters heatingPrincipleDiagramPrameters = new HeatingPrincipleDiagramPrameters();
					heatingPrincipleDiagramPrameters.isSameDisOut = (option.MainPipeInter == 1);
					heatingPrincipleDiagramPrameters.isSameDisIn = (option.RiserPipeInter == 1);
					heatingPrincipleDiagramPrameters.stanPipeCount = buildSystem.RiserPipeNum;
					heatingPrincipleDiagramPrameters.dic = dictionary;
					heatingPrincipleDiagramPrameters.dicLevelMarkInfo = dictionary2;
					XYZ basepoint = sel.PickPoint("选择一个插入点");
					new DrawSampleChart(doc, doc.ActiveView).DrawThePipeSampleChart(basepoint, lineNames, heatingPrincipleDiagramPrameters);
				}
			}
			catch (Autodesk.Revit.Exceptions.OperationCanceledException)
			{
			}
		}

		private string[] GetLineNames(Document doc)
		{
			string[] array = new string[]
			{
				"旋转轴",
				"<房间分隔>",
				"<面积边界>",
				"线",
				"隐藏线",
				"细线"
			};
			List<string> list = (from gs in new FloorPipeTools(doc).GetLineStyle()
			select gs.Name).ToList<string>();
			if (!list.Contains("线"))
			{
				return null;
			}
			if (!list.Contains("旋转轴"))
			{
				array[0] = "线";
			}
			if (!list.Contains("<房间分隔>"))
			{
				array[1] = "线";
			}
			if (!list.Contains("<面积边界>"))
			{
				array[2] = "线";
			}
			if (!list.Contains("隐藏线"))
			{
				array[4] = "线";
			}
			if (!list.Contains("细线"))
			{
				array[5] = "线";
			}
			return array;
		}
	}
}
